<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2017/8/27
 * Time: 20:34
 */

namespace Home\Logic;


use Common\Utils\Constants;
use Home\Model\Page;

class MatchLogLogic
{
    public function saveMatchLog($user_id,$opponent_id,$room,$model){
        $log['create_time'] = time();
        $log['room_id'] = $room['id'];
        $log['money_type'] = $room['money_type'];
        $log['integral'] = $room['integral'];
        $log['level'] = $room['level'];
        $log['model'] = $room['model'];
        $log['user_id'] = $user_id;
        $log['opponent_id'] = $opponent_id;
        $log['status'] = 0;
        $model->table('t_match_log')->add($log);
    }

    public function getMatchLogList($map,$pagenum=1,$pagesize=20){
        $startno = ($pagenum - 1) * $pagesize;
        $limit = $startno . ',' . $pagesize;//每页的数据数和内容$limit
        $map['t_match_log.is_delete'] = 0;
        $matchLogList = D('MatchLog')->join('LEFT JOIN t_user u ON u.id=t_match_log.user_id')
            ->join('LEFT JOIN t_user ou ON ou.id=t_match_log.opponent_id')
            ->field('ou.nickname as opponent_nickname,ou.head_url as o_head_url,ou.sex as opponent_sex,u.sex as user_sex,u.nickname as user_nickname,u.head_url as head_url,t_match_log.*')
            ->where($map)
            ->order('id desc')->limit($limit)
            ->select();
        foreach ($matchLogList as $key => $value){
            $matchLogList[$key]['create_time'] = date('y/m/d',$value['create_time']);
        }
        $data['list'] = $matchLogList;
        $count = D('MatchLog')->where($map)->count();
        $page = new Page($pagenum, $count, $pagesize);
        $page = (array)$page;//转换为数组
        $data['page'] = $page;
        return $data;
    }

    /**
     * 获取用户对战场次
     * @param $user_id int 用户id
     */
    public function getBattleTimes($user_id){
        $where['t_match_log.user_id'] = $user_id;
        $where['t_match_log.opponent_id'] = $user_id;
        $where['_logic'] = 'OR';
        $map['_complex'] = $where; //and
        $map['t_match_log.status']  = Constants::MATCH_END;
        $count = D('MatchLog')->where($map)->count();
        return $count;
    }

    /**
     * 获取比赛记录 并且显示为公告
     */
    public function getMatchLogListByNotice($pagenum=1,$pagesize=10){
        $startno = ($pagenum - 1) * $pagesize;
        $limit = $startno . ',' . $pagesize;//每页的数据数和内容$limit
        $map['status'] = Constants::MATCH_END;
        $matchLogList = D('MatchLog')->join('LEFT JOIN t_user u ON u.id=t_match_log.winner')
            ->field('u.nickname as nickname,t_match_log.*')
            ->where($map)
            ->order('t_match_log.id desc')->limit($limit)
            ->select();
        $notice = array();
        foreach ($matchLogList as $key=>$value){
            if($value['opponent_id'] == $value['winner']){
                $type = '打擂';
            }else{
                $type = '守擂';
            }
            $notice[$key]['title'] = $value['nickname'].$type.'成功获得'.round($value['reward_money']);
            if($value['money_type'] == Constants::MONEY_TYPE_GOLD){
                $notice[$key]['title'] .='金币';
            }else{
                $notice[$key]['title'] .='钻石';
            }
        }
        return $notice;
    }
}